package com.iha.pongzor.gui;

import com.iha.pongzor.*;
import com.iha.pongzor.logic.HighscoreChecker;
import com.iha.pongzor.logic.myMediaPlayer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

public class Menu extends Activity {
	/** Called when the activity is first created. */

	myMediaPlayer mediaplayer;
	MediaPlayer mediaPlayer;
	AssetFileDescriptor descriptor;
	boolean musicPlay;
	OnSharedPreferenceChangeListener listener;
	SharedPreferences prefs;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.menu);
	}

	private void initBtnPlay() {        
        Button btnPong = (Button) findViewById(R.id.menu_btn_play);
        btnPong.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// start contact activity
				startActivity(new Intent(v.getContext(), GameCoverFlow.class));
			}
		});
	}

	private void initPrefListener() {

		listener = new OnSharedPreferenceChangeListener() {

			private Context context;

			public OnSharedPreferenceChangeListener init(Context _context) {
				context = _context;
				return this;
			}

			public void onSharedPreferenceChanged(
					SharedPreferences sharedPreferences, String key) {

				if (key.compareTo("music_checkbox") == 0) {
					musicPlay = sharedPreferences.getBoolean(key, true);

					if (musicPlay == false) {
						mediaplayer.stop();
					}

					if (musicPlay == true) {
						int numberChoosen = Integer.parseInt(prefs.getString("track_list","1"));
						
						if (numberChoosen == 2)
							mediaplayer.changeNumber(getString(R.string.track_2));
						else
							mediaplayer.changeNumber(getString(R.string.track_1));
						
						mediaplayer.start();
					}
				}

				if (key.compareTo("track_list") == 0) {
					int numberChoosen = Integer.parseInt(sharedPreferences
							.getString(key, "1"));

					if (numberChoosen == 1) {
						mediaplayer
								.changeNumber(getString(R.string.track_1));
						if (musicPlay) {
							mediaplayer.stop();
							mediaplayer.start();
						}
					} else if (numberChoosen == 2) {
						mediaplayer.changeNumber(getString(R.string.track_2));
						if (musicPlay) {
							mediaplayer.stop();
							mediaplayer.start();
						}
					}
				}

				if (key.compareTo("highscore_service_checkbox") == 0) {
					boolean service = sharedPreferences.getBoolean(key, true);
					if (service)
						startService(new Intent(context, HighscoreChecker.class));
					else
						stopService(new Intent(context, HighscoreChecker.class));
				}
			}
		}.init(this);

		prefs.registerOnSharedPreferenceChangeListener(listener);
	}

	private void initBtnHP() {
		Button btnHP = (Button) findViewById(R.id.menu_btn_contact);
		btnHP.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// start contact activity
				startActivity(new Intent(v.getContext(), Contact.class));
			}
		});
	}

	private void initBtnHS() {
		Button btnHS = (Button) findViewById(R.id.menu_btn_highscore);
		btnHS.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// start highscore activity
				startActivity(new Intent(v.getContext(),
						HighscoreActivity.class));
			}
		});

		Button btnDownloader = (Button) findViewById(R.id.menu_btn_tester);
		btnDownloader.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				startActivity(new Intent(v.getContext(), Downloader.class));
			}
		});
	}

	private void startMusic() {
		setVolumeControlStream(AudioManager.STREAM_MUSIC);
		if (musicPlay) {
			int numberChoosen = Integer.parseInt(prefs.getString("track_list",
					"1"));
			if (numberChoosen == 2)
				mediaplayer.changeNumber(getString(R.string.track_2));
			else
				mediaplayer.changeNumber(getString(R.string.track_1));
			mediaplayer.start();
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		if (mediaPlayer != null) {
			mediaPlayer.start();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		prefs = PreferenceManager.getDefaultSharedPreferences(this);

		musicPlay = prefs.getBoolean("music_checkbox", true);
		mediaplayer = new myMediaPlayer(getAssets());
		
		startService(new Intent(this, HighscoreChecker.class));

		startMusic();

		initBtnHS();
		initBtnHP();
		initBtnPlay();

		initPrefListener();
	}

	protected void onDestroy() {
		super.onPause();
		mediaplayer.stop();
		if (isFinishing())
			mediaplayer.destroy();
	}
	
	public boolean onCreateOptionsMenu(android.view.Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.developer_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		Intent i;
		switch (item.getItemId()) {
		case R.id.developerMenu_add_highscores:
			i = new Intent(this, HighscoreActivity.class).putExtra("InitializeDB", true);			
			startActivity(i);
			return true;
		case R.id.developerMenu_clear_highscores:
			i = new Intent(this, HighscoreActivity.class).putExtra("ClearDB", true);			
			startActivity(i);
			return true;
		case R.id.developerMenu_Preferences:
			i = new Intent(this, Preferences.class);			
			startActivity(i);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}